package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Qyfr;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.organ
 * 类描述:    企业法人dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */

@Repository
public class QyfrDao extends SimpleHibernateTemplate<Qyfr, Long> {
    /**
     * 查询企业法人列表
     *
     * @param qyqc     企业全称
     * @param fddlr    法定代表人
     * @param nsh      纳税号
     * @param pageNo   页数
     * @param pageSize 每页显示记录数
     * @return
     */
    public Page<Qyfr> findQyfrList(String qyqc, String fddlr, String nsh, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Qyfr> page = new Page<Qyfr>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qyqc != null && !qyqc.isEmpty()) {
            condition.append(" qyqc like ? ");
            param.add("%" + qyqc.trim() + "%");
        }
        if (fddlr != null && !fddlr.isEmpty()) {
            condition.append(" fddlr like ? ");
            param.add("%" + fddlr.trim() + "%");
        }
        if (nsh != null && !nsh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nsh = ? ");
            param.add(nsh);
        }
        //==============记录数量===============
        hql = "select count(*) from Qyfr";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Qyfr ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    /**
     * 判断企业法人属性是否重复
     *
     * @param id            企业法人Id
     * @param propertyValue 企业法人属性值
     * @param propertyName  企业法人属性名
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Qyfr where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Qyfr where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

    /**
     * 查询内部企业法人
     *
     * @return
     */
    public List<Qyfr> findAllNbQyfr() {
        return find("from Qyfr where   lx = 1");
    }
    /**
     * 查询外部企业法人
     *
     * @return
     */
    public List<Qyfr> findAllWbQyfr() {
        return find("from Qyfr where   lx = 0");
    }

    public List<Map> findByJcOrQc(String qyqc, String qyjc, Long id, Integer maxRows) {
        String hql = "select new map(id as id,qyqc as qyqc,qyjc as qyjc,fddlr as fddlr,dh as dh,nsh as nsh) from Qyfr";
        ArrayList param = new ArrayList();
        StringBuilder where = new StringBuilder();
        if (StringUtils.isNotEmpty(qyqc)) {
            if (where.length() != 0)
                where.append(" or ");
            where.append("qyqc like ?");
            param.add(qyqc + "%");
        }
        if (StringUtils.isNotEmpty(qyjc)) {
            if (where.length() != 0)
                where.append(" or ");
            where.append("qyjc like ?");
            param.add(qyjc + "%");
        }
        if (id != null) {
            if (where.length() != 0)
                where.append(" or ");
            where.append("id=?");
            param.add(id);
        }
        hql += (where.length() != 0 ? " where " : "") + where;
        return createQuery(hql, param.toArray()).setMaxResults((maxRows != null ? maxRows : 20)).setFirstResult(0).list();
    }

    public String getQyfrTreeJson() {
        try {
            String nodes = "[";
            StringBuffer json = new StringBuffer();
            String hql = "from Qyfr  where lx = 1 ";
            List<Qyfr> result = find(hql);
            if (result != null && !result.isEmpty()) {
                for (int i = 0; i < result.size(); i++) {
                    Qyfr q = result.get(i);
                    json.append("{\"id\":\"");
                    json.append(q.getId());
                    json.append("\",\"text\":\"");
                    json.append(q.getQyqc());
                    json.append("\",\"iconCls\":\"icon-tree-nbbm");
                    json.append("\",\"attributes\":{\"id\":\"" + q.getId() + "\"}");
                    json.append("},");
                }
                nodes += json.substring(0, json.length() - 1);
            }
            nodes += "]";
            return nodes;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }
}
